package A3GCPackage;

import java.util.List;

import org.jgroups.Address;
import org.jgroups.JChannel;

public class NodeRanker 
{
	protected NodeData nodes;
	protected RankingScheme rankingScheme;
	
	public NodeRanker( JChannel channel )
	{
		nodes = new NodeData(channel);
		rankingScheme = RankingSchemeFactory.getSystemRankingScheme();
	}
	
	public SingleNodeData getAvailableNode( String groupName )
	{
		List<SingleNodeData> output;
		
		// zero object should be the best fit
		output = rankingScheme.rank(nodes, groupName); 
		if ( output.size() == 0 )
			return null;
		return output.get(0); 
	}
	
	public void setNodeActivity( String name, boolean active, String groupName, Address address )
	{
		nodes.setNodeActivity(name, active, groupName, address);
	}
	
	public void addNodeIfNotThere( String name, String groupName, Address address )
	{
		nodes.addNodeIfNotExistent(name, groupName, address);
	}
	
	public void deleteNodeIfSameGroup( String name, String groupName )
	{
		nodes.deleteNodeIfSameGroup(name, groupName);
	}
}
